package cn.ac.iscas.config;


import cn.ac.iscas.base.constant.WebSocketConstant;
import cn.ac.iscas.domain.common.User;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;

/**
 *用户拦截器
 *
 * @author admin*/
@SuppressWarnings("rawtypes")
public class UserInterceptor implements ChannelInterceptor {
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public Message<?> preSend(@Nonnull Message<?> message, @Nonnull MessageChannel channel) {

        StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        assert accessor != null;
        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            Object raw = message.getHeaders().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);
            if (raw instanceof Map) {
                //这里就是token
                Object name = ((Map) raw).get(WebSocketConstant.TOKEN_KEY);
                if (name instanceof List) {
                    // 设置当前访问器的认证用户
                    String token = ((List) name).get(0).toString();

//                    String username = null;
//                    try {
//                        Map<String, Claim> claimMap = JWTUtils.verifyToken(token);
//                        username = claimMap.get("username").asString();
//                        if(username == null){
//                            throw new RuntimeException("websocket认证失败");
//                        }
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                        throw new RuntimeException("websocket认证失败", e);
//                    } catch (ValidTokenException e) {
//                        e.printStackTrace();
//                        throw new RuntimeException("websocket认证失败", e);
//                    }
//                    User user = new User();
//                    user.setUsername(username);
//                    accessor.setUser(user);

                    User user = new User();
                    user.setUsername(token);
                    accessor.setUser(user);

                }
            }
        } else if (StompCommand.SEND.equals(accessor.getCommand())) {
            //发送数据

        }

        return message;
    }

    @Override
    public void postSend(@Nonnull Message<?> message, @Nonnull MessageChannel channel, boolean sent) {

    }

    @Override
    public void afterSendCompletion(@Nonnull Message<?> message, @Nonnull MessageChannel channel, boolean sent, Exception ex) {

    }

    @Override
    public boolean preReceive(@Nonnull MessageChannel channel) {
        return false;
    }

    @Override
    public Message<?> postReceive(@Nonnull Message<?> message, @Nonnull MessageChannel channel) {
        return null;
    }

    @Override
    public void afterReceiveCompletion(Message<?> message, @Nonnull MessageChannel channel, Exception ex) {

    }
}
